<template>
  <views>
    <div class="enterprise_office_sign" ref="enterprise_office_sign">
      <viewTitle :title="'人员签约记录'" :border="true"></viewTitle>
      <div class="tables">
        <el-form ref="form" label-width="auto" :inline="true">
          <div :class="search_list1">
            <!-- 人员编号 -->
            <div class="serial_number flex flexR center">
              <el-form-item label="人员编号:">
                <el-input v-model="form.professional_sn" placeholder="人员编号" :clearable="true"></el-input>
              </el-form-item>
            </div>
            <!-- 创建时间 -->
            <div class="creation_time flex flexR center">
              <el-form-item label="创建时间:">
                <el-date-picker
                  v-model="form.creation_time"
                  value-format="timestamp"
                  type="datetimerange"
                  align="right"
                  :default-time="['0:00:00','23:59:59']"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions"
                ></el-date-picker>
              </el-form-item>
            </div>

          </div>
        </el-form>
        <!-- 关键字  查询 重置  导出列表 -->
        <el-form ref="form" label-width="auto" :inline="true">
        <div :class="search_list2">
          <!-- 签约状态 -->
          <div class="contract_status flex flexR center">
            <el-form-item label="签约状态:">
              <el-select v-model="form.is_contract" clearable placeholder="请选择">
                <el-option
                        v-for="item in form.contract_status_list"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>
          <!-- 认证状态 -->
          <div class="contract_status flex flexR center">
            <el-form-item label="认证状态:">
              <el-select v-model="form.is_auth" clearable placeholder="请选择">
                <el-option
                        v-for="item in form.auth_list"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="name_phone_card flex flexR center">
            <el-form-item label="关键字:">
            <el-input v-model="form.keyword" placeholder="请输入姓名/手机号/身份证号/银行卡号" :clearable="true"></el-input>
            </el-form-item>
          </div>
        </div>
          <div class="search_input flex flexR center">
            <el-form-item >
              <div class="name_phone_card_btn flex flexR flexJp">
                <el-button type="primary" @click="query" icon="el-icon-search">查询</el-button>
                <el-button type="danger" @click="reset" icon="el-icon-refresh">重置</el-button>
                <el-button
                        type="primary"
                        @click="export_list"
                        :style="{width:140+'px'}"
                        size="medium"
                        icon="el-icon-download"
                >导出列表</el-button>
              </div>
            </el-form-item>
        </div>
        </el-form>
      </div>
      <div class="table_list">
        <el-table
          border
          :data="tableData"
          style="width: 100%;max-width:none;"
          :header-cell-style="getRowClass"
          :select-on-indeterminate="true"
          :fit="true"
          :lazy="true"
          @row-click="clickRow"
          max-height="450"
        >
          <el-table-column fixed="left" type="index" label="序号" width="50"></el-table-column>
          <el-table-column prop="professional_sn" label="人员编号" width="100"></el-table-column>
          <el-table-column prop="name" label="姓名"></el-table-column>
          <el-table-column prop="cer_code" label="身份证" width="180"></el-table-column>
          <el-table-column prop="bank_code" label="银行卡号" width="180"></el-table-column>
          <el-table-column prop="mobile" label="预留手机号码" width="120"></el-table-column>
          <el-table-column prop="facilitator_name" label="签约服务商" width="150"></el-table-column>
          <el-table-column prop="enterprise_professional_facilitator_sn" label="签约编号" width="150"></el-table-column>
          <el-table-column prop="contract_start_time" label="开始日期" width="160"></el-table-column>
          <el-table-column prop="contract_end_time" label="终止日期" width="160"></el-table-column>
          <el-table-column
            prop="is_contract"
            :formatter="(row) => { let text = ['未签约','已签约','已解约']; return text[row.is_contract]}"
            label="签约状态"
            width="100"
          ></el-table-column>
          <el-table-column prop="is_auth" label="认证状态" width="100"></el-table-column>
          <el-table-column prop="created_time" label="创建时间" width="160"></el-table-column>
          <el-table-column fixed="right" label="操作" width="260" v-if="isLook || isContractDo || isContractSave">
            <template slot-scope="scope" class="table_operation">
              <el-button
                type="primary"
                @click="look(scope.row.enterprise_professional_facilitator_id)"
                v-show="isLook"
              >详情</el-button>
              <el-button
                v-if="parseInt(scope.row.is_contract) == 1 && isContractDo"
                type="warning"
                @click="do_contract(scope.row.enterprise_professional_facilitator_id, scope.row.is_contract * 1)"
              >解约</el-button>
              <el-button
                v-if="(parseInt(scope.row.is_contract) == 0 || parseInt(scope.row.is_contract) == 2) && isContractDo"
                type="success"
                @click="do_contract(scope.row.enterprise_professional_facilitator_id, scope.row.is_contract * 1)"
              >签约</el-button>

              <el-button
                v-if="isContractSave"
                type="success"
                @click="amend(scope.row.enterprise_professional_facilitator_id, scope.row.is_contract * 1)"
              >修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination_num flex flexR center flexJp">
        <el-pagination
          style="background:#fff; padding:10px 0"
          background
          layout="total, sizes,prev, pager, next, jumper"
          @size-change="handleSizeChange($event)"
          @current-change="changePage($event)"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.page_size"
          :total="pagination.total_count"
          :current-page="pagination.current_page"
          :page-count="pagination.page_count"
        ></el-pagination>
      </div>
    </div>
  </views>
</template>


<script>
import  {mapGetters} from 'vuex'

export default {
  name: "signing",
  data() {
    return {
      form: {
        professional_sn: "", // 人员编号
        creation_time: "", // 创建时间
        contract_status_list: [
          { value: null, label: "全部" },
          { value: 0, label: "未签约" },
          { value: 1, label: "已签约" },
          { value: 2, label: "已解约" }
        ], // 签约状态列表
        auth_list: [
            { value: null, label: "全部" },
            { value: 0, label: "未认证" },
            { value: 1, label: "已认证" },
        ], // 认证状态列表
        is_contract: null, // 签约状态
        is_auth:null,//认证状态
        keyword: "" // 关键字
      },
      marginLeft: 0,
      search_list2: ["flex", "flexR", "search_list2"],
      search_list1: ["flex", "flexR", "search_list2"],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      tableData: [],
      pagination: {
        //分页数据
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      },
      download_url: "" //导出列表地址
    };
  },
  computed: {
    post_data() {
      let data = {};
      if (this.form.creation_time && this.form.creation_time.length > 0) {
        data.from_time = this.form.creation_time[0] / 1000; //去掉后面的毫秒级
        data.end_time = this.form.creation_time[1] / 1000; //去掉后面的毫秒级
      }
      data.professional_sn = this.form.professional_sn;
      data.keyword = this.form.keyword;
      if (this.form.is_contract !== null)
        data.is_contract = this.form.is_contract;

      if (this.form.is_auth !== null)
          data.is_auth = this.form.is_auth;
      data.pagination = this.pagination;
      return data;
    },
    ...mapGetters(['getButtonPermission']),
    isLook(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].look ? 1 : 0;
    },
    isContractDo(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].contract_do ? 1 : 0;
    },
    isContractSave(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].contract_save ? 1 : 0;
    }
  },

  methods: {
    //获取签约列表数据
    query() {
      let post_data = this.post_data;
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.e_contractList,
        post_data
      );
      if (code != 200) {
        this.$message.error(msg);
        this.tableData = [];
        return;
      }
      this.tableData = data.list;
      this.pagination = data.pagination;
      this.download_url = data.download_url;
      console.log("come table_data", data.list);
    },
    //导出列表
    export_list() {
      this.$import_excal(this.download_url || "");
    },

    //    修改第一行样式
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #FAFAFA ";
      } else {
        return "";
      }
    },
    //     点击单行
    clickRow(row, column, event) {
      console.log(row, column, event);
    },
    //    修改是否换行
    changeStyleRow(width) {
      let _width = width - 290 - 350 - 300 - 50 - 25;
      if (_width > 300) {
        this.marginLeft = 25;
      } else {
        this.marginLeft = 0;
      }
    },

    //  重置
    reset() {
      this.form.professional_sn = ""; // 人员编号
      this.form.creation_time = ""; // 创建时间
      this.form.sign_servicer = ""; // 签约服务商
      this.form.contract_status = null; // 签约状态
      this.form.is_contract = null; // 签约状态
      this.form.is_auth = null; // 认证状态
      this.form.keyword = ""; // 关键字
      this.query();
    },
    //查看签约详情
    look(enterprise_professional_facilitator_id) {
      this.$toRoutePath({
        path: "/enterprise_office_sign/enterprise_office_sign_detail",
        edit: true,
        query: {
          enterprise_professional_facilitator_id: enterprise_professional_facilitator_id,
          type: 1
        }
      });
    },
    // 修改
    amend(enterprise_professional_facilitator_id) {
      this.$toRoutePath({
        path: "/enterprise_office_sign/enterprise_office_sign_detail",
        edit: true,
        query: {
          enterprise_professional_facilitator_id: enterprise_professional_facilitator_id,
          type: 2
        }
      });
    },
    handleSizeChange(page_size) {
      //变动每页数量时
      this.pagination.page_size = page_size;
      this.query();
    },
    changePage(current_page) {
      //切换页码时
      this.pagination.page_start = current_page;
      this.query();
    },

    // 签约/解约
    do_contract(enterprise_professional_facilitator_id, is_contract) {
      let content_title = "";
      if (is_contract == 0 || is_contract == 2) content_title = "签约";
      if (is_contract == 1) content_title = "解约";
      this.$confirm("确认" + content_title + "该人员?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let { code, msg } = this.$api.sendSync(this.$mapapi.e_contractDo, {
            enterprise_professional_facilitator_id
          });
          if (code != 200) {
            this.$message.error(msg);
            return;
          }
          this.$message.success(msg);
          this.query();
        })
        .catch(() => {
          this.$message.error("取消" + content_title);
        });
    }
  },

  mounted() {
    this.$setRouteTitle();
    this.changeStyleRow(this.$refs.enterprise_office_sign.clientWidth);
    console.log("comoe mouned");
    this.query();
  }
};
</script>



<style lang="less" :scope="true">
.enterprise_office_sign {
  padding: 15px;
  background: #ffffff;
  margin-bottom: 20px;
  .serial_number {
    width: 310px;
  }
  .creation_time,
  .sign_servicer,
  /*.contract_status {*/
    /*margin-left: 25px;*/
  /*}*/
  .creation_time {
    width: auto;
  }
  .sign_servicer,
  .contract_status {
    width: 310px;
  }
  .search_list1 {
  }
  .search_list2 {
    height: auto;
  }
  .input-title {
    width: 85px;
    font-size: 15px;
  }
  .search_list2 .name_phone_card {
    width: 370px;
    .el-input__inner{
      width: 270px;
      padding:0 10px;
    }
  }
  .name_phone_card_btn {
    width: 350px;
    margin-left: 25px;
  }
  .tables {
    margin-top: 0px;
  }
  .pagination_num {
    height: 120px;
  }
  .table_list {
    margin: 0px 15px;

    .table_operation {
      margin: 0 auto;
      justify-content: space-around;
    }
  }
}
</style>